package com.zyw.mvc.servlet;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 静态资源controller
 *
 * @author zhangyw
 * @date 2018/2/5 9:46
 */
@Controller
public class PageNavController extends BaseController {

    /**
     * 页面后缀
     */
    private static final String pageSuffix = ".html";

//    /**
//     * 设置默认欢迎页
//     */
//    @RequestMapping(value = "/", method = RequestMethod.GET)
//    public ModelAndView navToHome(HttpServletRequest request, HttpServletResponse response) {
//        return new ModelAndView("redirect:/index.html");
//    }

    /**
     * 通用页面跳转
     */
    @RequestMapping(value = "/**/*" + pageSuffix, method = RequestMethod.GET)
    public ModelAndView navTo(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("basePath", request.getContextPath());
        String reqUri = request.getRequestURI();
        mav.setViewName(reqUri.substring(request.getContextPath().length() + 1, reqUri.indexOf(pageSuffix)));
        return mav;
    }
}
